<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class History_m extends MY_Model {

	public function __construct()
	{
		parent::__construct();
	}
	
	function convertion(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "time >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "time <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = "user_id = ".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			if(!$cond2){
				$cond = "(". (string) implode(' AND ', $tempArray2) .")"; 
			}else{
				$cond = "(". (string) implode(' AND ', $tempArray2) .") OR ".$cond2; 
			}		
		}
		
		/**		
		if(! $cond AND $cond2){
			$total = count( $this->db->where($cond2)->get(TBL_CONVERSION)->result() );
		}else if(! $cond AND !$cond2){
			$total = count( $this->db->get(TBL_CONVERSION)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_CONVERSION." WHERE $cond ")->result() );
		}	
		**/
		
		$user_id = ($USER) ? $USER->getId():0;
		$cond = " time >= $fromDateStamp AND time <= $toDateStamp AND user_id = ".$user_id;
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/history/convertion/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
		/**					
		if(! $cond AND $cond2) {
			$records = $this->db->where($cond2)->order_by('time','desc')->limit($records_per_page, $per_page )->get(TBL_CONVERSION)->result();
		}else if(! $cond AND !$cond2){
			$records = $this->db->order_by('time','desc')->limit($records_per_page, $per_page )->get(TBL_CONVERSION)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_CONVERSION." WHERE {$cond} ORDER BY time DESC LIMIT {$per_page},{$records_per_page}")->result();
		}	
		**/
		$records = $this->db->query("SELECT * FROM ".TBL_CONVERSION." WHERE {$cond} ORDER BY time DESC LIMIT {$per_page},{$records_per_page}")->result();
			
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );	
	}
	
	function bet(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "bet_time >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "bet_time <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = "user_id = ".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			if(!$cond2){
				$cond = "(". (string) implode(' AND ', $tempArray2) .")"; 
			}else{
				$cond = "(". (string) implode(' AND ', $tempArray2) .") OR ".$cond2; 
			}	
		}
	/**			
		if(! $cond AND $cond2){
			$total = count( $this->db->where($cond2)->get(TBL_BET)->result() );
		}else if(! $cond AND !$cond2){
			$total = count( $this->db->get(TBL_BET)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_BET." WHERE $cond ")->result() );
		}
	**/		
		$user_id = ($USER) ? $USER->getId():0;
		$cond = " bet_time >= $fromDateStamp AND bet_time <= $toDateStamp AND user_id = ".$user_id;	
		$total = count( $this->db->query("SELECT * FROM ".TBL_BET." WHERE $cond ")->result() );
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/history/bet/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	/**						
		if(! $cond AND $cond2) {
			$records = $this->db->where($cond2)->order_by('bet_time','desc')->limit($records_per_page, $per_page )->get(TBL_BET)->result();
		}else if(! $cond AND !$cond2){
			$records = $this->db->order_by('bet_time','desc')->limit($records_per_page, $per_page )->get(TBL_BET)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_BET." WHERE {$cond} ORDER BY bet_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		}
	**/
		$records = $this->db->query("SELECT * FROM ".TBL_BET." WHERE {$cond} ORDER BY bet_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );	
	}
	
	function bet_manual(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "bet_time >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "bet_time <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = "user_id = ".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			if(!$cond2){
				$cond = "(". (string) implode(' AND ', $tempArray2) .")"; 
			}else{
				$cond = "(". (string) implode(' AND ', $tempArray2) .") OR ".$cond2; 
			}	
		}
	/**			
		if(! $cond AND $cond2){
			$total = count( $this->db->where($cond2)->get(TBL_BET_MANUAL)->result() );
		}else if(! $cond AND !$cond2){
			$total = count( $this->db->get(TBL_BET_MANUAL)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_BET_MANUAL." WHERE $cond ")->result() );
		}	
	**/
		$user_id = ($USER) ? $USER->getId():0;
		$cond = " bet_time >= $fromDateStamp AND bet_time <= $toDateStamp AND user_id = ".$user_id;	
		$total = count( $this->db->query("SELECT * FROM ".TBL_BET_MANUAL." WHERE $cond ")->result() );
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/history/bet_manual/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	/*						
		if(! $cond AND $cond2) {
			$records = $this->db->where($cond2)->order_by('bet_time','desc')->limit($records_per_page, $per_page )->get(TBL_BET_MANUAL)->result();
		}else if(! $cond AND !$cond2){
			$records = $this->db->order_by('bet_time','desc')->limit($records_per_page, $per_page )->get(TBL_BET_MANUAL)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_BET_MANUAL." WHERE {$cond} ORDER BY bet_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		}	
	*/		
		$records = $this->db->query("SELECT * FROM ".TBL_BET_MANUAL." WHERE {$cond} ORDER BY bet_time DESC LIMIT {$per_page},{$records_per_page}")->result();
			
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );	
	}
	
	function tournament_bet(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "bet_time >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "bet_time <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = "user_id = ".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			if(!$cond2){
				$cond = "(". (string) implode(' AND ', $tempArray2) .")"; 
			}else{
				$cond = "(". (string) implode(' AND ', $tempArray2) .") OR ".$cond2; 
			}	
		}
	/**			
		if(! $cond AND $cond2){
			$total = count( $this->db->where($cond2)->get(TBL_BET_TOURNAMENT)->result() );
		}else if(! $cond AND !$cond2){
			$total = count( $this->db->get(TBL_BET_TOURNAMENT)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_BET_TOURNAMENT." WHERE $cond ")->result() );
		}	
	**/
		$user_id = ($USER) ? $USER->getId():0;
		$cond = " bet_time >= $fromDateStamp AND bet_time <= $toDateStamp AND user_id = ".$user_id;	
		$total = count( $this->db->query("SELECT * FROM ".TBL_BET_TOURNAMENT." WHERE $cond ")->result() );
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/history/tournament_bet/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	/**						
		if(! $cond AND $cond2) {
			$records = $this->db->where($cond2)->order_by('bet_time','desc')->limit($records_per_page, $per_page )->get(TBL_BET_TOURNAMENT)->result();
		}else if(! $cond AND !$cond2){
			$records = $this->db->order_by('bet_time','desc')->limit($records_per_page, $per_page )->get(TBL_BET_TOURNAMENT)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_BET_TOURNAMENT." WHERE {$cond} ORDER BY bet_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		}
	*/	
		$records = $this->db->query("SELECT * FROM ".TBL_BET_TOURNAMENT." WHERE {$cond} ORDER BY bet_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );	
	}
	
	function tournament_bet_manual(){
		$fromDate = $this->input->get('fromDate',TRUE);
		$toDate   = $this->input->get('toDate',TRUE);
		$username	  = $this->input->get('username',TRUE);
		$fromDateStamp = convertToTimeStampFromFormat($fromDate,"m/d/Y");
		$toDateStamp = convertToTimeStampFromFormat($toDate,"m/d/Y");
		
		$USER = $this->user_entity->init('username',$username);
	 
		if($fromDateStamp != 0){
			$condFrom = "bet_time >= $fromDateStamp";
		}else{
			$condFrom = "";
		}
		
		if($toDateStamp != 0){
			$condTo = "bet_time <= $toDateStamp";
		}else{
			$condTo = "";
		}
		
		if( $USER ){
			$cond2 = "user_id = ".$USER->getId();
		}else{
			$cond2 = "";
		}
		
		if(!$condFrom AND !$condTo ){
			$cond = ""; 
		}else{
			$tempArray = array('condFrom'=>$condFrom,'condTo'=>$condTo);
			$tempArray2 = array();
			foreach($tempArray as $key=>$val){
				if($val){
					$tempArray2[] = "$val";
				}
			}
			if(!$cond2){
				$cond = "(". (string) implode(' AND ', $tempArray2) .")"; 
			}else{
				$cond = "(". (string) implode(' AND ', $tempArray2) .") OR ".$cond2; 
			}	
		}
	/*			
		if(! $cond AND $cond2){
			$total = count( $this->db->where($cond2)->get(TBL_BET_MANUAL_TOURNAMENT)->result() );
		}else if(! $cond AND !$cond2){
			$total = count( $this->db->get(TBL_BET_MANUAL_TOURNAMENT)->result() );
		}else{
			$total = count( $this->db->query("SELECT * FROM ".TBL_BET_MANUAL_TOURNAMENT." WHERE $cond ")->result() );
		}	
	*/
		$user_id = ($USER) ? $USER->getId():0;
		$cond = " bet_time >= $fromDateStamp AND bet_time <= $toDateStamp AND user_id = ".$user_id;	
		$total = count( $this->db->query("SELECT * FROM ".TBL_BET_MANUAL_TOURNAMENT." WHERE $cond ")->result() );
		
		$records_per_page = 25;
		$per_page = intval ( $this->input->get('per_page',TRUE) );
		$per_page = ( $per_page > 0) ? $per_page : 0; 
		
		$pagination = create_pagination( 
								$uri = "admin/dataman/history/tournament_bet_manual/?fromDate=$fromDate&toDate=$toDate&username=$username", 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	/**						
		if(! $cond AND $cond2) {
			$records = $this->db->where($cond2)->order_by('bet_time','desc')->limit($records_per_page, $per_page )->get(TBL_BET_MANUAL_TOURNAMENT)->result();
		}else if(! $cond AND !$cond2){
			$records = $this->db->order_by('bet_time','desc')->limit($records_per_page, $per_page )->get(TBL_BET_MANUAL_TOURNAMENT)->result();
		}else{
			$records = $this->db->query("SELECT * FROM ".TBL_BET_MANUAL_TOURNAMENT." WHERE {$cond} ORDER BY bet_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		}
	**/
		$records = $this->db->query("SELECT * FROM ".TBL_BET_MANUAL_TOURNAMENT." WHERE {$cond} ORDER BY bet_time DESC LIMIT {$per_page},{$records_per_page}")->result();
		
		return array('pagination'=> $pagination, 'records'=>$records, 'total_records'=>$total );
	}
	
	
//end class
}	